﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucColdStorageLoadingStandard.xaml
    /// </summary>
    public partial class ucColdStorageLoadingStandard : UserControl
    {
        List<int> products = new List<int>();
        Boolean check = true;
        public ucColdStorageLoadingStandard()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }
        public void loadData()
        {
            
            cbxProduct.ItemsSource=null;
            cbxProduct.Items.Clear();
            foreach (var item in LibraryManagerInCom.LoadingStandardList)
            {
                var chkproduct = 0;
                chkproduct = products.SingleOrDefault(j => j == item.product_id);
                if (chkproduct == 0)
                {
                    tb_product p = LibraryManagerInCom.ProductList.SingleOrDefault(i => i.id == item.product_id);

                    cbxProduct.Items.Add(p);
                    //products.Add(p.id);
                }

            }            
            cbxProduct.DisplayMemberPath = "product_name";
            cbxProduct.SelectedIndex = 0;
        }

     

       
        public void loadProduct()
        {
            cbxProduct.Items.Clear();
            cbxProduct.ItemsSource = LibraryManagerInCom.ProductList;
            cbxProduct.DisplayMemberPath = "product_name";
            cbxProduct.SelectedIndex = 0;
            check = false;
        }

        private void cbxProduct_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int productID;
            if (cbxProduct.SelectedItem != null && check)
            {
                productID = int.Parse(((tb_product)cbxProduct.SelectedItem).id.ToString());
                tb_loading_standard ls = LibraryManagerInCom.LoadingStandardList.SingleOrDefault(i => i.product_id == productID);
                txtLoadingStandard.Text = ls.loading_standard.ToString();
            }

        }

        public void delData()
        {
            tb_product p = (tb_product)cbxProduct.SelectedItem;
            tb_loading_standard ls = LibraryManagerInCom.LoadingStandardList.SingleOrDefault(i => i.product_id == p.id);
            LibraryManagerInCom.delLoadingStandard(ls);
            loadData();
        }
        public void newData()
        {
            loadProduct();
            txtLoadingStandard.Text = "";
        }
        public void saveData()
        {
            tb_product p = (tb_product)cbxProduct.SelectedItem;
            tb_loading_standard ls = new tb_loading_standard();
            ls.product_id = p.id;
            ls.loading_standard = Convert.ToDouble(txtLoadingStandard.Text);
            LibraryManagerInCom.addLoadingStandard(ls);
            loadData();
        }

        public void copyData()
        {
            tb_loading_standard ls = LibraryManagerInCom.copyLoadingStandard((tb_loading_standard)cbxProduct.SelectedItem);
            //cbxProduct.Text = ls.;
            cbxProduct.SelectedItem = LibraryManagerInCom.ProductList.SingleOrDefault(i => i.id == ls.product_id);
            txtLoadingStandard.Text = ls.loading_standard.ToString();
            cbxProduct.IsEditable = true;
        }


        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }
}
